//
//  LoginSuccessViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/19.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "LoginSuccessViewController.h"
#import "Success_labelTableViewCell.h"
#import "SuccessTableViewCell.h"
#import "SuccessHeadView.h"
#import "UIImageView+WebCache.h"
#import "SuccessSetViewController.h"

#import "LoginDataData.h"
#import "LoginData.h"
#import "LoginRootClass.h"
#import "LoginDataRootClass.h"
#define kTableViewLabelReuse @"LabelReuse"
#define kTableViewReiuse @"reuse"
@interface LoginSuccessViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *successTableView;
@property (nonatomic, strong) LoginRootClass *rootModel;
@property (nonatomic, strong) LoginDataRootClass *DataModel;
@end

@implementation LoginSuccessViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    // 使状态栏颜色为白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
   
    self.successTableView.bounces = NO;
}


//-(void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.hidden = YES;
//    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
//    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
//    // 是的状态栏颜色为黑色
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.successTableView registerNib:[UINib nibWithNibName:@"SuccessTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewReiuse];
    [self.successTableView registerNib:[UINib nibWithNibName:@"Success_labelTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewLabelReuse];
    self.navigationItem.title = @"我的";
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = [UIColor whiteColor];
    label.text = @"我的";
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    
    
    UIBarButtonItem *sharebtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icons_share_white"] style:UIBarButtonItemStyleDone target:self action:@selector(handleShare:)];
    UIBarButtonItem *setBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"set_icon@"] style:UIBarButtonItemStyleDone target:self action:@selector(handleSet:)];
    
    self.navigationItem.rightBarButtonItems = @[setBtn, sharebtn];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage new] style:UIBarButtonItemStyleDone target:self action:@selector(handleLeft:)];
    
}
- (void)handleLeft:(UIBarButtonItem *)btn {
    // 这个目的是去除左边的按钮
}

- (void)setData:(LoginDataData *)data {
    _data = data;
    [self.successTableView reloadData];
}

- (void)handleShare:(UIBarButtonItem *)btn {
    
    
}
- (void)handleSet:(UIBarButtonItem *)btn {
    self.hidesBottomBarWhenPushed = YES;
    SuccessSetViewController *vc = [[SuccessSetViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    } else if (section == 1) {
        return 2;
    } else {
        return 3;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            SuccessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReiuse];
            cell.picImageView.image = [UIImage imageNamed:@"today_icon_8"];
            cell.titleLabel.text = @"我的订单";
            
            return cell;
        } else if (indexPath.row == 0){
            Success_labelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewLabelReuse];
            cell.picImageView.image = [UIImage imageNamed:@"project_heart_full"];
            cell.titleLabel.text = @"感兴趣的活动";
            cell.numLabel.text = @"0";
            
            return cell;
        } else if (indexPath.row ==2) {
            Success_labelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewLabelReuse];
            cell.picImageView.image = [UIImage imageNamed:@"today_icon_9"];
            cell.titleLabel.text = @"我的优惠劵";
            cell.numLabel.text = @"0";
            
            return cell;
        } else {
            Success_labelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewLabelReuse];
            cell.picImageView.image = [UIImage imageNamed:@"today_icon_3"];
            cell.titleLabel.text = @"我关注的商家";
            cell.numLabel.text = @"0";
            return cell;
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            SuccessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReiuse];
            cell.picImageView.image = [UIImage imageNamed:@"today_icon_5"];
            cell.titleLabel.text = @"我发布的现场";
            return cell;
        } else {
            SuccessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReiuse];
            cell.picImageView.image = [UIImage imageNamed:@"today_icon_7"];
            cell.titleLabel.text = @"我的提问";
            return cell;
        }
       

    } else {
        if (indexPath.row == 0) {
            SuccessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReiuse];
            cell.picImageView.image = [UIImage imageNamed:@"today_icon_11"];
            cell.titleLabel.text = @"推荐给好友";
            return cell;
        } else if (indexPath.row == 1) {
            Success_labelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewLabelReuse];
            cell.picImageView.image = [UIImage imageNamed:@"today_icon_1"];
            cell.titleLabel.text = @"客服电话";
            cell.numLabel.text = @"4006610153";
            return cell;
        } else {
            Success_labelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewLabelReuse];
            cell.picImageView.image = [UIImage imageNamed:@"today_icon_4"];
            cell.titleLabel.text = @"我要成为商家";
            cell.numLabel.text = @"免费入驻";
            return cell;
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        // 从本地取出数据
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSDictionary *dic = [user objectForKey:@"LoginData"];
        if (dic != nil) {
            self.DataModel = [LoginDataRootClass modelWithDic:dic];
            SuccessHeadView *headView = [[SuccessHeadView alloc] init];
            [headView.picImageView sd_setImageWithURL:[NSURL URLWithString:self.DataModel.data.avatar] placeholderImage:[UIImage imageNamed:@"login_yonghu_boy"]];
            headView.userLabel.text = self.DataModel.data.name;
            if (self.DataModel.data.gender == 1) {
                NSString *sex = @"男:";
                headView.ageAndSexlabel.text = [sex stringByAppendingFormat:@"%ld岁", self.DataModel.data.age];
            } else {
                NSString *sex = @"女:";
                headView.ageAndSexlabel.text = [sex stringByAppendingFormat:@"%ld岁", self.DataModel.data.age];
            }
            //        headView.ageAndSexlabel.text = @"男:25岁";
            headView.attentionLabel.text = @"关注";
            headView.numOfattention.text = [NSString stringWithFormat:@"%ld", self.DataModel.data.followcount];
            headView.fansLabel.text = @"粉丝";
            headView.numOfFans.text =[NSString stringWithFormat:@"%ld", self.DataModel.data.fanscount];
            
            headView.funnyLabel.text = @"感兴趣";
            headView.numOfFunny.text = [NSString stringWithFormat:@"%ld", self.DataModel.data.likercount];
            
            return headView;
        } else {
            SuccessHeadView *headView = [[SuccessHeadView alloc] init];
            [headView.picImageView sd_setImageWithURL:[NSURL URLWithString:_data.avatar] placeholderImage:[UIImage imageNamed:@"login_yonghu_boy"]];
            headView.userLabel.text = _data.name;
            if (_data.gender == 1) {
                NSString *sex = @"男:";
                headView.ageAndSexlabel.text = [sex stringByAppendingFormat:@"%ld岁", _data.age];
            } else {
                NSString *sex = @"女:";
                headView.ageAndSexlabel.text = [sex stringByAppendingFormat:@"%ld岁", _data.age];
            }
            //        headView.ageAndSexlabel.text = @"男:25岁";
            headView.attentionLabel.text = @"关注";
            headView.numOfattention.text = [NSString stringWithFormat:@"%ld", _data.followcount];
            headView.fansLabel.text = @"粉丝";
            headView.numOfFans.text =[NSString stringWithFormat:@"%ld", _data.fanscount];
            
            headView.funnyLabel.text = @"感兴趣";
            headView.numOfFunny.text = [NSString stringWithFormat:@"%ld", _data.likercount];
            
            return headView;
        }
        

    } else {
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor colorWithRed:0.922 green:0.922 blue:0.945 alpha:1.000];
        
        return view;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 250;
    } else {
        return 30;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
